<?php
class UserController extends CBaseController
{
	
	protected $user;
	protected $validator;
	
	public function __construct($config=null) 
    { 
      parent::__construct($config);	  
    } 
	
	public function init($tpl)
	{
		parent::init($tpl);
		$this->reqLogin();
		$this->templator->addViewsDir(dirname(__FILE__).'/../views');
	}
	
	public function all()
	{
	 $users = R::findAll('userdetail',' ORDER BY firstname');
	 $this->templator->assign('users',array_merge($users, array()));
	 $this->templator->display('user_list.html');
	}
	
	public function savePrepare()
	{
		$this->user = R::dispense('userdetail');
		$this->user->username = $_POST['username'];
		$this->user->firstname = $_POST['firstname'];
		$this->user->lastname =  $_POST['lastname'];
		$this->user->email = $_POST['email'];
		$this->user->pass = $_POST['pass'];
		$this->user->type = $_POST['type'];
	}
	
	public function updatePrepare()
	{
		$id = $_POST['id'];
		$this->user = R::load('userdetail',$id);
		$this->user->username = $_POST['username'];
		$this->user->firstname = $_POST['firstname'];
		$this->user->lastname =  $_POST['lastname'];	
		$this->user->email = $_POST['email'];		
		if(!isset($_POST['pass']) || trim($_POST['pass'])==='')
		{
			$this->user->pass = $_POST['pass'];
		}		
		$this->user->type = $_POST['type'];
	}
	
	public function saveORupdateValidate()
	{
		$this->validator = new FormValidator();
		$this->validator->addValidation("username","req","Please fill in Name");
		$this->validator->addValidation("username","regexp=/^[A-Z0-9]{2,30}$/i","Please enter your valid username!");
		$this->validator->addValidation("email","email","The input for Email should be a valid email value");
		$this->validator->addValidation("email","req","Please fill in Email");
		$this->validator->addValidation("first_name","regexp=/^[A-Z \'.-]{2,20}$/i","Please enter your valid first name!");
		$this->validator->addValidation("last_name","regexp=/^[A-Z \'.-]{2,40}$/i","Please enter your valid last name!");		
		return $this->validator->ValidateForm();
	}
		
	
	public function create()
	{
		$this->user = R::dispense('userdetail');
		$this->templator->assignByRef('user',$this->user);
		$this->templator->display('user_general.html');
	}
	public function save()
	{
	  $this->savePrepare();
	  if($this->saveORupdateValidate())
	  {
		R::store($this->user);
		$this->templator->assign('info','User added sucessfully');
		$this->templator->assignByRef('user',$this->user);		
		$this->templator->display('user_general.html');	
	  }
	  else
	  {
		$error_hash = $this->validator->GetErrors();
		$this->templator->assign('validate_errors',$error_hash);
		$this->templator->assignByRef('user',$this->user);
		$this->templator->display('user_general.html');
	  }
	}
	
	public function edit()
	{
		$id = $_GET['id'];
		if(isset($id))
		$this->user = R::load('userdetail',$id);
		else
		$this->user = R::dispense('userdetail');
		$this->templator->assignByRef('user',$this->user);		
		$this->templator->display('user_general.html');
	}
	
	public function update()
	{
		$this->updatePrepare();
		  if($this->saveORupdateValidate())
		  {
			R::store($this->user);
			$this->templator->assignByRef('user',$this->user);
			$this->templator->assign('info','User updated sucessfully');
			$this->templator->display('user_general.html');	
		  }
		  else
		  {
			$error_hash = $this->validator->GetErrors();
			$this->templator->assign('validate_errors',$error_hash);
			$this->templator->assignByRef('user',$this->user);
			$this->templator->display('user_general.html');
		  }
	}
	
	public function delete()
	{
	 $id = $_GET['id'];
	 if(isset($id))
	 {
		$user = R::load('userdetail',$id);
		if(isset($user))
		{
			R::trash($user);
			$this->templator->assign('info','User deleted sucessfully');
		}
	 }
	 $this->all();
	}
}	